//
//  ViewController.m
//  block
//
//  Created by yhj on 16/1/7.
//  Copyright © 2016年 QQ:1787354782. All rights reserved.
//  直接传值

#import "ViewController.h"
#import "TwoVC.h"
#import "oneVC.h"
#import "Masonry.h"

@interface ViewController ()<TwoVCDelegate>

@property(nonatomic,strong)UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor purpleColor];
    self.title=@"A界面";
    
        // NSMutableParagraphStyle
        NSMutableParagraphStyle *paragraphStyle=[[NSMutableParagraphStyle alloc]init];
        
        // 首行缩进  1个字符8个字节 1个汉子相当于2～4字符
        paragraphStyle.firstLineHeadIndent=32.0;
        paragraphStyle.lineHeightMultiple =1.5;
        paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
        NSDictionary *attribute=@{
         NSParagraphStyleAttributeName:paragraphStyle,
         NSForegroundColorAttributeName:[UIColor blackColor],
         NSFontAttributeName:[UIFont systemFontOfSize:18],
         //NSBackgroundColorAttributeName:[UIColor yellowColor],
         NSWritingDirectionAttributeName:@[@(NSWritingDirectionLeftToRight)],
         };

    UILabel *label=[[UILabel alloc]init];
    label.backgroundColor=[UIColor redColor];
    label.numberOfLines=0;
    label.text=@"此代码主要是区别代理和Block传值,描述如下:A和B两个界面,由A界面 “点击按钮” 跳转到B界面,在B界面内的textField输入内容xx,点击“返回按钮”,那么刚才输入的内容xx,就显示在A界面的label。";
    label.attributedText=[[NSAttributedString alloc]initWithString:label.text attributes:attribute];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(55);
        make.left.mas_equalTo(self.view).offset(10);
        make.right.mas_equalTo(self.view).offset(-10);
        make.height.mas_equalTo(240);
    }];
    
    // 传值显示在label
    _label=[[UILabel alloc]init];
    [self.view addSubview:_label];
    _label.backgroundColor=[UIColor yellowColor];
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(label.mas_bottom).offset(40);
        make.left.mas_equalTo(self.view).offset(60);
        make.right.mas_equalTo(self.view).offset(-50);
        make.height.mas_equalTo(40);
    }];
    
    UIButton *btn=[[UIButton alloc]init];
    btn.backgroundColor=[UIColor blueColor];
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_label.mas_bottom).offset(10*2);
        make.left.mas_equalTo(self.view).offset(100);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(50);
    }];
}

/*** 通过“协议或代理” 实现两个页面之间的传值 ***/
-(void)btnClick
{
    TwoVC *two=[TwoVC new];
    two.delegate=self;  // 设置代理
    [self.navigationController pushViewController:two animated:YES];
}

// 实现协议TwoVCDelegate中的方法
-(void)passTextValue:(NSString *)textField
{
    _label.text=textField;
}



/***  block作为property实现两个页面之间的传值 ***/
//-(void)btnClick
//{
//    oneVC *one=[oneVC new];
//    one.NextVCBlock=^(NSString *textField){
//        [self resetLabel:textField];
//    };
//    [self.navigationController pushViewController:one animated:YES];
//}

//-(void)btnClick
//{
//    oneVC *one=[oneVC new];
//    [one nextVCBlock:^(NSString *textField) {
//        [self resetLabel:textField];
//    }];
//    [self.navigationController pushViewController:one animated:YES];
//}
//
//-(void)resetLabel:(NSString *)textField
//{
//   _label.text=textField;
//}

@end
